# -*- coding:utf-8 -*-
from com.zzh.utils.jsonUtils_v2.table2Json_Zhao import Table2JsonZhao
from com.zzh.utils.requestFieldMapDBField import fieldMap_Zhao
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils


class ViewCategoryItemDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def find_by_condition(self, **kwargs):
        """
        view_item 条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        # 获取对应的映射字典
        fieldMap = fieldMap_Zhao.viewCategoryItemFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM view_category_items WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM view_category_items WHERE " + whereSentence
        try:
            rs = cur.execute(sql)
        except:
            return -200, 0  # sql 语句执行错误

        if rs:
            adinfo = cur.fetchall()
            lists = MyPaging().getPage(rs, pageIndex, pageSize, adinfo)
            adJson = Table2JsonZhao.view_category_item_list_to_json(lists)
            return adJson, rs
        else:
            return None, 0